#include <cmath>
#include <iostream>
using namespace std;
#include "vector.h"
using std::atan2;
using std::cos;
using std::sin;

namespace VECTOR {
const double Rad_to_deg = 57.2957795130823;

void Vector::set_mag() { _mag = sqrt(_x * _x + _y * _y); }
void Vector::set_ang() {
  if (_x == 0.0 && _y == 0.0) {
    _ang = 0.0;
  } else {
    _ang = atan2(_y, _x);
  }
}

void Vector::set_x() { _x = _mag * cos(_ang); }
void Vector::set_y() { _y =_mag * sin(_ang); }

Vector::Vector() { _x = _y = _mag = _ang = 0.0; }

Vector::Vector(double n1, double n2, char from) {
  _mode = from;
  if (from == 'r') {
    _x = n1;
    _y = n2;
    set_mag();
    set_ang();
  } else if (from == 'p') {
    _mag = n1;
    _ang = n2 / Rad_to_deg;
    set_x();
    set_y();
  } else {
    cout << "vector set to 0" << endl;
    _x = _y = _mag = _ang = 0.0;
    _mode = 'r';
  }
}

void Vector::set(double n1, double n2, char from) {
  _mode = from;
  if (from == 'r') {
    _x = n1;
    _y = n2;
    set_mag();
    set_ang();
  } else if (from == 'p') {
    _mag = n1;
    _ang = n2 / Rad_to_deg;
    set_x();
    set_y();
  } else {
    cout << "vector set to 0" << endl;
    _x = _y = _mag = _ang = 0.0;
    _mode = 'r';
  }
}

Vector::~Vector() {}

void Vector::polar_mode() { _mode = 'p'; }
void Vector::rect_mode() { _mode = 'r'; }
Vector Vector::operator+(const Vector &b) const {
  return Vector(_x + b._x, _y + b._y);
}
Vector Vector::operator-(const Vector& b)const{
    return Vector(_x-b._x,_y-b._y);
}
Vector Vector::operator-()const{
    return Vector(-_x,-_y);
}
Vector Vector:: operator*(double n)const{
    return Vector(_x*n,_y*n);
}
Vector operator*(double n,const Vector& a){
    return a* n;
}
std::ostream& operator<<(std::ostream& os,const Vector& v){
    if(v._mode == 'r'){
        os<<"(x,y) = ("<<v._x<<","<<v._y<<")";
    }else if(v._mode == 'p'){
        os<<"(m,a)=("<<v._mag<<","<<v._ang*Rad_to_deg<<")";
    }else{
        os<<"invalid!"<<endl;
    }
    return os;
}
} // namespace VECTOR
